package com.hc.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hc.common.bean.PageBean;
import com.hc.common.bean.Result;
import com.hc.common.bean.ResultUtil;
import com.hc.constant.SkuConst;
import com.hc.constant.UserConst;
import com.hc.domain.Comment;
import com.hc.domain.OrderItem;
import com.hc.domain.Recomment;
import com.hc.domain.User;
import com.hc.query.CommentItem;
import com.hc.query.CommentVO;
import com.hc.res.CommentWithTag;
import com.hc.res.UserComment;
import com.hc.res.UserComment2;
import com.hc.service.Comment2Service;
import com.hc.service.CommentService;
import com.hc.service.OrderItemService;
import com.hc.service.RecommentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.io.Serializable;
import java.util.List;

/**
 * @author hc
 * @date 2021/12/23 16:04
 */
@Slf4j
@RestController
@RequestMapping("/comment")
public class CommentController {

    @Resource
    private CommentService commentService;

    @PostMapping
    public Result create(@RequestBody Comment comment) {
        boolean res = commentService.save(comment);
        return res ? ResultUtil.success("添加成功") : ResultUtil.fail("添加失败");
    }

    @PutMapping
    public Result updateById(@RequestBody Comment comment) {
        boolean res = commentService.updateById(comment);
        return res ? ResultUtil.success("更新成功") : ResultUtil.fail("更新失败");
    }

    @DeleteMapping("{id}")
    public Result removeById(@PathVariable Serializable id) {
        boolean res = commentService.removeById(id);
        return res ? ResultUtil.success("删除成功") : ResultUtil.fail("删除失败");
    }

    /**
     * @param id
     * @return
     */
    @GetMapping("{id}")
    public Result getById(@PathVariable Serializable id) {
        Comment res = commentService.getById(id);
        return ResultUtil.success("查询成功").setData(res);
    }

    //@PostMapping
    //public Result getByCondition(@RequestBody CommentQuery condition) {
    //    List<Comment> list = commentService.queryByCondition(condition);
    //    return ResultUtil.success("查询成功").setData(list);
    //}

    @Resource
    private Comment2Service comment2Service;


    /**
     * 查看评论的详情
     * 返回当前评论的详细信息以及对这条评论的所有的回复信息
     *
     * @param commentId
     * @return
     */
    @GetMapping("/getCommentWithRecommentsByCommentId/{commentId}/{pageNum}/{pageSize}")
    public Result getCommentWithRecommentsByCommentId(@PathVariable("commentId") Long commentId,
                                                      @PathVariable("pageNum") Integer pageNum,
                                                      @PathVariable("pageSize") Integer pageSize) {
        //获取用户信息+评论信息
        UserComment userComment = commentService.getUserComment(commentId);
        //获取回复的用户信息+评论信息
        PageBean<UserComment2> userComment2PageBean = comment2Service.getUserComment2ByCommentId(commentId, pageNum, pageSize);

        return ResultUtil.success().setData("userComment", userComment, "userComment2PageBean", userComment2PageBean);
    }

    /**
     * 我的评论
     *
     * @param pageNum
     * @param pageSize
     * @return
     */
    @GetMapping("/getCommentByUserId")
    public Result getCommentByUserId(Integer pageNum, Integer pageSize) {
        User user = UserConst.user;
        PageBean<CommentWithTag> pageBean = commentService.getCommentWithTag(pageNum, pageSize, user.getId());

        return ResultUtil.success().setData(pageBean);
    }

    @Resource
    private OrderItemService orderItemService;


    /**
     * 发表评论
     *
     * @param commentVO
     * @return
     */
    @GetMapping("publishComment")
    public Result publishComment(@RequestBody CommentVO commentVO) {
        List<CommentItem> commentItemList = commentVO.getCommentItemList();
        for (CommentItem commentItem : commentItemList) {
            //先判断订单上的评论的条数是否大于1，如果大于1，则不能发表
            OrderItem tmp = orderItemService.getOne(new QueryWrapper<OrderItem>()
                    .eq("order_id", commentVO.getOrderId())
                    .eq("sku_id", commentItem.getSkuId()));
            Integer comments = tmp.getComments();
            if (comments >= SkuConst.Comment.FIRST) {
                return ResultUtil.fail("已经发布过评论了").setData("{\"times\":" + comments + "}");
            }
        }
        //发表评论
        commentService.saveComment(commentVO);
        return ResultUtil.success("评论成功");
    }


    @Resource
    private RecommentService recommentService;

    /**
     * 追加评论
     *
     * @param recomment
     * @param orderId
     * @return
     */
    @GetMapping("republishComment/{orderId}/{skuId}")
    public Result republishComment(@RequestBody Recomment recomment,
                                   @PathVariable("orderId") String orderId,
                                   @PathVariable("skuId") Integer skuId) {
        //先判断订单上的评论的条数是否大于1，如果大于1，则不能发表
        OrderItem tmp = orderItemService.getOne(new QueryWrapper<OrderItem>()
                .eq("order_id", orderId)
                .eq("sku_id", skuId));
        Integer comments = tmp.getComments();

        if (comments != SkuConst.Comment.FIRST) {
            return ResultUtil.fail("已发布或从未发布过评论").setData("{\"times\":" + comments + "}");
        }
        User user = UserConst.user;

        recommentService.saveRecomment(recomment, orderId, skuId, user.getId());

        return ResultUtil.success("评论成功");
    }


}